package com.yougou.ui.category.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.yougou.R;
import com.yougou.data.model.Goods;

import java.util.ArrayList;
import java.util.List;

public class CategoryGoodsAdapter extends RecyclerView.Adapter<CategoryGoodsAdapter.ViewHolder> {
    
    private List<Goods> mDataList = new ArrayList<>();
    private OnItemClickListener mOnItemClickListener;
    
    public void setData(List<Goods> dataList) {
        if (dataList != null) {
            mDataList = dataList;
            notifyDataSetChanged();
        }
    }
    
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
    
    public interface OnItemClickListener {
        void onItemClick(Goods goods);
    }
    
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_goods, parent, false);
        return new ViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Goods goods = mDataList.get(position);
        
        holder.tvName.setText(goods.getGoodsName());
        holder.tvPrice.setText(String.format("¥%.2f", goods.getGoodsPrice()));
        
        // 加载商品图片
        String imageUrl = goods.getGoodsSmallLogo();
        if (imageUrl == null || imageUrl.trim().isEmpty()) {
            imageUrl = goods.getGoodsBigLogo();
        }
        
        // 如果图片URL仍然为空，直接显示占位图，不调用Glide加载
        if (imageUrl != null && !imageUrl.trim().isEmpty()) {
            Glide.with(holder.itemView.getContext())
                    .load(imageUrl)
                    .placeholder(R.drawable.placeholder_image)
                    .error(R.drawable.placeholder_image)
                    .into(holder.ivGoods);
        } else {
            // URL为空，直接设置占位图
            holder.ivGoods.setImageResource(R.drawable.placeholder_image);
        }
        
        // 点击事件
        holder.itemView.setOnClickListener(v -> {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(goods);
            }
        });
    }
    
    @Override
    public int getItemCount() {
        return mDataList.size();
    }
    
    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivGoods;
        TextView tvName;
        TextView tvPrice;
        
        ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivGoods = itemView.findViewById(R.id.iv_goods);
            tvName = itemView.findViewById(R.id.tv_goods_name);
            tvPrice = itemView.findViewById(R.id.tv_goods_price);
        }
    }
}

